package com.luhuiguo.cms.server.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.luhuiguo.cms.server.service.ArticleService;
import com.luhuiguo.cms.server.service.AttachmentService;
import com.luhuiguo.cms.server.service.ChannelService;
import com.luhuiguo.cms.server.service.CmsService;
import com.luhuiguo.cms.server.service.restriction.ArticleFilter;
import com.luhuiguo.cms.server.service.restriction.AttachmentFilter;
import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.server.util.PathUtils;
import com.luhuiguo.cms.shared.model.ArticleModel;
import com.luhuiguo.cms.shared.model.AttachmentModel;
import com.luhuiguo.cms.shared.model.ChannelModel;

@Service("cms")
public class CmsServiceImpl implements CmsService {

    @Inject
    private ChannelService channelService;

    @Inject
    private ArticleService articleService;

    @Inject
    private AttachmentService attachmentService;

    @Override
    public String abbreviate(String str, int maxWidth) {
        if (maxWidth > 3) {
            return StringUtils.abbreviate(str, maxWidth);
        } else {
            return str;
        }
    }

    @Override
    public String resolvePath(String base, String path) {
        
        String str = PathUtils.resolvePath(base, path);
        if (!str.endsWith("/")) {
            str += "/";
        }
        
        return str;
        
        
    }

    @Override
    public ChannelModel resolveChannel(ChannelModel base, String path) {
        if (".".equals(path)||"".equals(path)) {
            return base;
        } else {
            ChannelModel c = getChannelByPath(resolvePath(base.getPath(), path));
            if (null == c) {
                return base;
            } else {
                return c;
            }
        }

    }

    @Override
    public ChannelModel getChannel(Long id) {
        return MapperUtils.map(channelService.getChannel(id), ChannelModel.class);
    }

    @Override
    public ChannelModel getChannelByPath(String path) {
        return MapperUtils.map(channelService.getChannelByPath(path), ChannelModel.class);
    }

    @Override
    public ArticleModel getArticle(Long id) {
        return MapperUtils.map(articleService.getArticle(id), ArticleModel.class);
    }

    @Override
    public String getName() {
        return "LuLuCMS";
    }

    @Override
    public List<AttachmentModel> getAttachments(Long articleId) {
        return MapperUtils.mapList(attachmentService.findAttachmentByArticleIdAndType(articleId,
                AttachmentModel.TYPE_ATTACH), AttachmentModel.class);
    }

    @Override
    public List<AttachmentModel> getImages(Long articleId) {
        return MapperUtils.mapList(attachmentService.findAttachmentByArticleIdAndType(articleId,
                AttachmentModel.TYPE_IMAGE), AttachmentModel.class);
    }

    @Override
    public List<ChannelModel> getChannels(Long parentId) {
        return MapperUtils.mapList(channelService.findChannelsByParentId(parentId), ChannelModel.class);
    }

    @Override
    public List<ChannelModel> getTopChannels(Long parentId, int limit) {
        return MapperUtils.mapList(channelService.findChannelsByParentId(parentId, 0, limit), ChannelModel.class);
    }

    @Override
    public List<ChannelModel> getBreadcrumb(Long channelId) {
        List<ChannelModel> list = new ArrayList<ChannelModel>();
        Long id = channelId;
        while (id != null) {
            ChannelModel c = getChannel(id);
            if (null != c) {
                id = c.getParentId();
                list.add(0, c);
            } else {
                id = null;
            }
        }

        return list;
    }

    @Override
    public ChannelModel getChannelByResolvePath(String base, String path) {
        return getChannelByPath(PathUtils.resolvePath(base, path));
    }

    @Override
    public List<ArticleModel> getArticles(Long channelId, int start, int limit) {
        ArticleFilter filter = new ArticleFilter();
        filter.setChannelId(channelId);
        filter.setStatus(ArticleModel.APPROVED);
        return MapperUtils.mapList(articleService.findArticles(filter, start, limit), ArticleModel.class);
    }

    @Override
    public int getArticleCount(Long channelId) {
        ArticleFilter filter = new ArticleFilter();
        filter.setChannelId(channelId);        
        filter.setStatus(ArticleModel.APPROVED);
        return articleService.getArticleCount(filter);
    }

    @Override
    public List<AttachmentModel> getSlideShow(int grade, int limit) {
        AttachmentFilter filter = new AttachmentFilter();
        filter.setType(AttachmentModel.TYPE_IMAGE);
        filter.setGrade(grade);

        return MapperUtils.mapList(attachmentService.findAttachments(filter, 0, limit), AttachmentModel.class);
    }

}